package red.stu.pconstraint.dto.mapper;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;
import java.util.Map;

/**
 * 流程分支
 * @author yihrmc @stu.red
 * @date 2019/11/15 9:15
 */
@Getter
@Setter
@ToString
@Accessors(chain = true)
public class Branch {

    public static final String MASTER_ID = "master";

    /**
     * 流程分支唯一ID <br/>
     *    - 主分支默认为 #MASTER_ID
     * @see Branch#MASTER_ID
     */
    @NotNull
    private String id;

    /**
     * 第一个分支节点
     */
    private BranchNode firstNode;

    /**
     * 最后一个分支节点
     */
    private BranchNode lastNode;

    /**
     * 分支拥有的所有节点
     */
    @NotNull
    private Map<String, BranchNode> nodes;

    /**
     * 分钟描述信息
     */
    @Nullable
    private String describe;

    public BranchNode getBranchNode(String nodeId) {
        BranchNode branchNode = getNodes().get(nodeId);
        if (branchNode == null) {
            throw new RuntimeException("BranchNode not find: " + nodeId);
        }
        return branchNode;
    }

    public boolean containsBranchNode(String nodeId) {
        if (getNodes() == null) {
            return false;
        }
        return getNodes().containsKey(nodeId);
    }

}
